var pageMod = require("page-mod");
var widgets = require("widget");
var tabs = require("tabs");
var data = require("self").data;

var ss = require("simple-storage");

function init_storage()
{
    if (!ss.storage.server)
        ss.storage.server = "";
    if (!ss.storage.user)
        ss.storage.user = "";
    if (!ss.storage.pass)
        ss.storage.pass = "";
    if (!ss.storage.autoconn)
        ss.storage.autoconn = 0;
    if (!ss.storage.autodisconn)
        ss.storage.autodisconn = 0;
    ss.storage.connected = 0;
}

init_storage();

function connect()
{
    var link_url;
    var link_content;
    var myIconURL = data.url('xx.png');

    if(ss.storage.server == "" || ss.storage.user == "" ||
    ss.storage.pass == "") {
        require("notifications").notify({
            title: 'Dr.com拨号状态',
            text: "Server,用户名,密码都不能为空!",
            iconURL: myIconURL
        });
        return;
    }

    if(ss.storage.connected == 0) {
        link_url = "http://"+ss.storage.server+"/";
        link_content = "DDDDD="+ss.storage.user+"&upass="+ss.storage.pass+"&0MKKey=%B5%C7%C2%BC+Login";

        require("request").Request({
            url: link_url,
            content: link_content,
            onComplete: function(response) {
                if(response.status == "200") {
                    ss.storage.connected = 1;
                    require("notifications").notify({
                        title: 'Dr.com拨号状态',
                        text: "目测拨号成功!",
                        iconURL: myIconURL
                    });
                } else {
                    require("notifications").notify({
                        title: 'Dr.com拨号状态',
                        text: "目测拨号没有成功!",
                        iconURL: myIconURL
                    });
                }
            }
        }).post();
    } else {
        link_url = "http://"+ss.storage.server+"/"+"F.htm";
        require("request").Request({
            url: link_url,
            onComplete: function(response) {
                if(response.status == "200") {
                    ss.storage.connected = 0;
                    require("notifications").notify({
                        title: 'Dr.com拨号状态',
                        text: "目测断开连接成功!",
                        iconURL: myIconURL
                    });
                } else {
                    require("notifications").notify({
                        title: 'Dr.com拨号状态',
                        text: "目测断开连接失败!",
                        iconURL: myIconURL
                    });
                }
            }
        }).get();
    }
}

var widget = widgets.Widget({
    label: "Dr.COM",
    id: "text-entry",
    contentURL: data.url("images/connected.png"),
    contentScriptFile: data.url("js/proc/click-listener.js")
});

widget.port.on("left-click", function(){
    connect();
});

widget.port.on("right-click", function(){
    tabs.open(data.url("config.html"));
});

function proc_msg_from_usr(data)
{
    var blocks=new Array();
    blocks = data.split('&');

    ss.storage.server = blocks[0];
    ss.storage.user = blocks[1];
    ss.storage.pass = blocks[2];
    ss.storage.autoconn = blocks[3];
    ss.storage.autodisconn = blocks[4];
}

pageMod.PageMod({
    include: "resource://drcom-at-sans/dr_com/data/config*",
    contentScriptFile: [
        data.url("js/jquery-1.7.1.js"),
        data.url("js/proc/config.js")
    ],
    onAttach: function(worker) {
        worker.port.emit('init_page',ss.storage);
        worker.port.on('click', function(data) {
            proc_msg_from_usr(data);
        });
        worker.port.on('send_me_init_msg',function() {
            worker.port.emit('init_page',ss.storage);
        });
    }
});
